﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Windows.Controls;
using Abreu.Base.ViewModels.Interfaces;
using System.ComponentModel;
using CorpAbreu.Common.BaseClasses;
//using Abreu.Base.ViewModels.Orchestrators;
using System.Reflection;
using System.Configuration;
using CorpAB.Views;
using CorpAbreu.Common.Models;
using Abreu.Base.ViewModels.Managers;

namespace Abreu.Base.ViewModels
{
    public class NavigationPaneVM : IViewModel
    {
        public ObservableCollection<TabItem> AbreuApplications { get; set; }

        public int ApplicationIndex { get; set; }

        public TabManagerVM TabManagerVM { get; set; }

        public NavigationPaneVM()
        {
            this.AbreuApplications = new ObservableCollection<TabItem>();
            MenuDataSource mnDS = new MenuDataSource();
            List<CorpAbreu.Common.Models.Menu> rootMenu = mnDS.GetMenuTree(6);
            SetTree(rootMenu);
        }

        public NavigationPaneVM(MainVM parent, List<CorpAbreu.Common.Models.Menu> menuList, TabManagerVM containerVM)
        {
            this.AbreuApplications = new ObservableCollection<TabItem>();
            this.TabManagerVM = containerVM ;
            this.Parent = parent;
            SetTree(menuList);
        }

        private void SetTree(List<CorpAbreu.Common.Models.Menu> menuList)
        {
            for (int i = 0; i < menuList.Count; i++)
            {
                MenuItensVM root = new MenuItensVM(menuList[i].Name);
                BuildChildrenNodes(menuList[i].Children, root);

                NavigationPaneItem fileNp = new NavigationPaneItem();
                fileNp.DataContext = root;

                this.AbreuApplications.Add(new TabItem { Header = menuList[i].Name, Content = fileNp });
            }
        }

        private void BuildChildrenNodes(List<CorpAbreu.Common.Models.Menu> list, MenuItensVM root)
        {
            for (int i = 0; i < list.Count; i++)
            {
                Dictionary<String, String> dicParams = new Dictionary<string, string>();
                dicParams = ChildHasProperties(list[i]);
                if (dicParams.Count > 0)
                {
                    MenuItensVM rootChild = new MenuItensVM(list[i].Name, root, new DelegateCommand(((MainVM)this.Parent).CreateInstance(dicParams[ConfigurationManager.AppSettings["ViewModelParent"].ToString()]),
                        dicParams[ConfigurationManager.AppSettings["MethodName"].ToString()], list[i].Name, dicParams[ConfigurationManager.AppSettings["ViewModel"].ToString()]));
                    rootChild.IsExpanded = true;
                    BuildChildrenNodes(list[i].Children, rootChild);
                }
                else
                {
                    MenuItensVM rootChild = new MenuItensVM(list[i].Name, root);
                    rootChild.IsExpanded = true;
                    BuildChildrenNodes(list[i].Children, rootChild);
                }
            }
        }

        private Dictionary<String, String> ChildHasProperties(CorpAbreu.Common.Models.Menu menu)
        {
            Dictionary<String, String> dicParams = new Dictionary<string, string>();

            if (menu.ApplicationProperty != null && menu.ApplicationProperty.Count > 0)
            {
                for (int i = 0; i < menu.ApplicationProperty.Count; i++)
                {
                    if (menu.ApplicationProperty[i].Name == ConfigurationManager.AppSettings["ViewModelParent"].ToString())
                    {
                        dicParams.Add(ConfigurationManager.AppSettings["ViewModelParent"].ToString(), menu.ApplicationProperty[i].Value); 
                    }
                    else if (menu.ApplicationProperty[i].Name == ConfigurationManager.AppSettings["MethodName"].ToString())
                    {
                        dicParams.Add(ConfigurationManager.AppSettings["MethodName"].ToString(), menu.ApplicationProperty[i].Value);
                    }
                    else if (menu.ApplicationProperty[i].Name == ConfigurationManager.AppSettings["ViewModel"].ToString())
                    {
                        dicParams.Add(ConfigurationManager.AppSettings["ViewModel"].ToString(), menu.ApplicationProperty[i].Value);
                    }
                }
            }
            else if (menu.MenuProperty != null && menu.MenuProperty.Count > 0)
            {
                for (int i = 0; i < menu.MenuProperty.Count; i++)
                {
                    //if (menu.MenuProperty[i].Name == ConfigurationManager.AppSettings["ViewModelParent"].ToString())
                    //{
                    //    dicParams.Add(ConfigurationManager.AppSettings["ViewModelParent"].ToString(), menu.MenuProperty[i].Value);
                    //}
                    //else if (menu.MenuProperty[i].Name == ConfigurationManager.AppSettings["MethodName"].ToString())
                    //{
                    //    dicParams.Add(ConfigurationManager.AppSettings["MethodName"].ToString(), menu.MenuProperty[i].Value);
                    //}
                    //else if (menu.MenuProperty[i].Name == ConfigurationManager.AppSettings["ViewModel"].ToString())
                    //{
                    //    dicParams.Add(ConfigurationManager.AppSettings["ViewModel"].ToString(), menu.MenuProperty[i].Value);
                    //}
                }
            }
            return dicParams;
        }

        public string Name
        {
            get { throw new NotImplementedException(); }
        }

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public IViewModel Parent
        {
            get;
            set;
        }

        
    }
}
